package insultGen.systemFiles;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class InsultGenActivity extends Activity {
	
	String versionName = null;
	int versonCode = 0;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
			versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
			versonCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
        
        setTitle("InsultGen Beta " + versionName);
        SharedPreferences pre = getPreferences(MODE_PRIVATE);
        if(!pre.getBoolean("started", false)){
        	firstUse(pre);
        }
        if(!pre.getString("version_name", "0.0.0").contentEquals(versionName) || pre.getInt("version_code", 0) != versonCode){
        	updateVocabulary(pre);
        }
        
        final Gen gen = new Gen();
        
        final EditText insultText = (EditText) findViewById(R.id.editText1);
        
        Button button = (Button) findViewById(R.id.genButton);
        button.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {	
				checkPending();
				insultText.setText(gen.insult(getVerb(), getNoun(), getAdj()));
			}
		});
        Button optionsButton = (Button) findViewById(R.id.options);
        optionsButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				checkPending();
				startActivity(new Intent(getApplication(), InsultOptions.class));
			}
		});
        Button addButton = (Button) findViewById(R.id.add_insult_button);
        addButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				checkPending();
				startActivity(new Intent(getApplication(), AddInsult.class));
			}
		});
        Button removeButton = (Button) findViewById(R.id.remove_button);
        removeButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				checkPending();
				startActivity(new Intent(getApplication(), RemoveInsult.class));
			}
		});
        Button send = (Button) findViewById(R.id.send_as_sms);
        send.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				checkPending();
				
				Options.transferMessage(insultText.getText().toString());
				
				startActivity(new Intent(getApplication(), SendMessage.class));
			}
		});
    }
    
    public String[] readLines(String file){
        InputStreamReader isr = null;
        FileInputStream fIn = null;
        BufferedReader br = null;
        String lineBuffer = null;
        List<String> buffer = new ArrayList<String>();
        try {
        	fIn = openFileInput(file);
        	isr = new InputStreamReader(fIn);
        	br = new BufferedReader(isr);
        	while ((lineBuffer = br.readLine()) != null) {
                buffer.add(lineBuffer);
            }
        } catch (Exception e) {
        	e.printStackTrace();
        }
        String[] data = buffer.toArray(new String[buffer.size()]);
        
        return data;
    }
    
    public void writeLines(String file, String[] words){
    	FileOutputStream fOut = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            fOut = openFileOutput(file, MODE_PRIVATE);
            osw = new OutputStreamWriter(fOut);
            bw = new BufferedWriter(osw);
            for(int i = 0; i < words.length; i++){
            	bw.write(words[i]);
            	if(i < words.length){
            		bw.newLine();
            	}
            }
            bw.flush();
            bw.close();
        }

        catch (Exception e) {

            e.printStackTrace();
        }
    }
    
    public String[] getVerb(){
    	
    	List<String> verbBuffer = new ArrayList<String>();
    	String[] temp = readLines("verb.txt");
    	for(int i = 0; i < temp.length; i++){
    		verbBuffer.add(temp[i]);
    	}
    	temp = null;
    	
    	temp = readLines("gender_" + Options.getGender() + "_verb.txt");
		for(int i = 0; i < temp.length; i++){
    		verbBuffer.add(temp[i]);
    	}
		temp = null;
		
    	if(Options.getRaceID() != R.id.none){
    		temp = readLines("race_" + Options.getRace() + "_verb.txt");
    		for(int i = 0; i < temp.length; i++){
        		verbBuffer.add(temp[i]);
        	}
    		temp = null;
    	}
    	 
    	String[] finalData = verbBuffer.toArray(new String[verbBuffer.size()]);
    	
    	return finalData;
    }
    
    public String[] getNoun(){
    	
    	List<String> nounBuffer = new ArrayList<String>();
    	String[] temp = readLines("noun.txt");
    	for(int i = 0; i < temp.length; i++){
    		nounBuffer.add(temp[i]);
    	}
    	temp = null;
    	
    	temp = readLines("gender_" + Options.getGender() + "_noun.txt");
		for(int i = 0; i < temp.length; i++){
    		nounBuffer.add(temp[i]);
    	}
		temp = null;
    	
    	if(Options.getRaceID() != R.id.none){
    		temp = readLines("race_" + Options.getRace() + "_noun.txt");
    		for(int i = 0; i < temp.length; i++){
        		nounBuffer.add(temp[i]);
        	}
    		temp = null;
    	}
    	
    	String[] finalData = nounBuffer.toArray(new String[nounBuffer.size()]);
    	
    	return finalData;
    }
    
    public String[] getAdj(){
    	
    	List<String> adjBuffer = new ArrayList<String>();
    	String[] temp = readLines("adj.txt");
    	for(int i = 0; i < temp.length; i++){
    		adjBuffer.add(temp[i]);
    	}
    	temp = null;
    	
    	temp = readLines("gender_" + Options.getGender() + "_adj.txt");
		for(int i = 0; i < temp.length; i++){
    		adjBuffer.add(temp[i]);
    	}
		temp = null;
    	
    	if(Options.getRaceID() != R.id.none){
    		temp = readLines("race_"+ Options.getRace() + "_adj.txt");
    		for(int i = 0; i < temp.length; i++){
        		adjBuffer.add(temp[i]);
        	}
    		temp = null;
    	}
    	
    	String[] finalData = adjBuffer.toArray(new String[adjBuffer.size()]);
    	
    	return finalData;
    }
    
    public void addAnInsult(String file, String line){
    	String[] buff = readLines(file);
    	List<String> data = new ArrayList<String>();
    	for(int i = 0; i < buff.length; i++){
    		data.add(buff[i]);
    	}
    	if(!data.contains(line)){
    		data.add(line);
    		System.out.println("bad");
    	}
    	String[] finalData = data.toArray(new String[data.size()]);
    	writeLines(file, finalData);
    }
    
    public void removeAnInsult(String line){
    	String[] files = {"verb.txt", 
    			"noun.txt",
    			"adj.txt",
    			"race_black_verb.txt",
    			"race_black_noun.txt",
    			"race_black_adj.txt",
    			"race_mexican_verb.txt",
    			"race_mexican_noun.txt",
    			"race_mexican_adj.txt",
    			"race_asain_verb.txt",
    			"race_asain_noun.txt",
    			"race_asain_adj.txt",
    			"race_white_verb.txt",
    			"race_white_noun.txt",
    			"race_white_adj.txt",
    			"gender_boy_verb.txt",
    			"gender_boy_noun.txt",
    			"gender_boy_adj.txt",
    			"gender_girl_verb.txt",
    			"gender_girl_noun.txt",
    			"gender_girl_adj.txt",};
    	;
    	for(int i = 0; i < files.length; i++){
    		String[] buff = readLines(files[i]);
    		List<String> data = new ArrayList<String>();
    		for(int i2 = 0; i2 < buff.length; i2++){
        		data.add(buff[i2]);
        	}
        	data.removeAll(Arrays.asList(line));
        	String[] finalData = data.toArray(new String[data.size()]);
        	writeLines(files[i], finalData);
        	data = null;
    	}
    }
    
    private void firstUse(SharedPreferences pre){
    	writeLines("verb.txt", Create.verbs());
    	writeLines("noun.txt", Create.nouns());
    	writeLines("adj.txt", Create.adjs());
    	writeLines("race_black_verb.txt", Create.race_black_verb());
    	writeLines("race_black_noun.txt", Create.race_black_noun());
    	writeLines("race_black_adj.txt", Create.race_black_adj());
    	writeLines("race_mexican_verb.txt", Create.race_mexican_verb());
    	writeLines("race_mexican_noun.txt", Create.race_mexican_noun());
    	writeLines("race_mexican_adj.txt", Create.race_mexican_adj());
    	writeLines("race_asian_verb.txt", Create.race_asian_verb());
    	writeLines("race_asian_noun.txt", Create.race_asian_noun());
    	writeLines("race_asian_adj.txt", Create.race_asian_adj());
    	writeLines("race_white_verb.txt", Create.race_white_verb());
    	writeLines("race_white_noun.txt", Create.race_white_noun());
    	writeLines("race_white_adj.txt", Create.race_white_adj());
    	writeLines("gender_boy_verb.txt", Create.gender_boy_verb());
    	writeLines("gender_boy_noun.txt", Create.gender_boy_noun());
    	writeLines("gender_boy_adj.txt", Create.gender_boy_adj());
    	writeLines("gender_girl_verb.txt", Create.gender_girl_verb());
    	writeLines("gender_girl_noun.txt", Create.gender_girl_noun());
    	writeLines("gender_girl_adj.txt", Create.gender_girl_adj());
    	SharedPreferences.Editor editpre = pre.edit();
    	editpre.putBoolean("started", true);
    	editpre.putString("version_name", versionName);
    	editpre.putInt("version_code",versonCode);
    	editpre.commit();
    }
    
    public void updateVocabulary(SharedPreferences pre){
    	String[] newVocabulary = null;
    	
    	newVocabulary = Create.verbs();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("verb.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	newVocabulary = Create.nouns();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("noun.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	newVocabulary = Create.adjs();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("adj.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	newVocabulary = Create.race_black_verb();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("race_black_verb.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	newVocabulary = Create.race_black_noun();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("race_black_noun.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	newVocabulary = Create.race_black_adj();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("race_black_adj.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	newVocabulary = Create.race_mexican_verb();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("race_mexican_verb.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	newVocabulary = Create.race_mexican_noun();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("race_mexican_noun.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	newVocabulary = Create.race_mexican_adj();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("race_mexican_adj.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	newVocabulary = Create.race_asian_verb();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("race_asian_verb.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	newVocabulary = Create.race_asian_noun();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("race_asian_noun.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	newVocabulary = Create.race_asian_adj();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("race_asian_adj.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	newVocabulary = Create.race_white_verb();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("race_white_verb.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	newVocabulary = Create.race_white_noun();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("race_white_noun.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	newVocabulary = Create.race_white_adj();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("race_white_adj.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	newVocabulary = Create.gender_boy_verb();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("gender_boy_verb.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	newVocabulary = Create.gender_boy_noun();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("gender_boy_noun.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	newVocabulary = Create.gender_boy_adj();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("gender_boy_adj.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	newVocabulary = Create.gender_girl_verb();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("gender_girl_verb.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	newVocabulary = Create.gender_girl_noun();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("gender_girl_noun.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	newVocabulary = Create.gender_girl_adj();
    	for (int i = 0; i < newVocabulary.length; i++){
    		addAnInsult("gender_girl_adj.txt", newVocabulary[i]);
    	}
    	newVocabulary = null;
    	
    	SharedPreferences.Editor editpre = pre.edit();
    	editpre.putBoolean("started", true);
    	editpre.putString("version_name", versionName);
    	editpre.putInt("version_code",versonCode);
    	editpre.commit();
    }
    
    public void checkPending(){
    	if(Options.isPending()){
    		addAnInsult(Options.getPendingFile(), Options.getPendingInsult());
    		Options.isAddPended();
    	}
    	if(Options.isRemovePending()){
    		removeAnInsult(Options.getPendingInsult());
    		Options.isRemovePended();
    	}
    }
}